package com.authine.cloudpivot.web.api.quartz.task;

import com.alibaba.dubbo.config.annotation.Reference;
import com.authine.cloudpivot.engine.api.facade.TaskReportFacade;
import com.authine.cloudpivot.web.api.dao.UserMapper;
import com.authine.cloudpivot.web.api.service.impl.FescoadQueryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class RecruitmentTraceJob implements Job {
    @Reference(version = "1.00")
    private TaskReportFacade taskReportFacade;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FescoadQueryServiceImpl fescoadQueryService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("招聘跟踪开始------------------------------------");
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        int days = (Integer) jobDataMap.get("data1");
        List<Map<String, Object>> listMap = taskReportFacade.traceRecruitment(days);

        Calendar cal = Calendar.getInstance();
        for (Map<String, Object> map : listMap) {
            String projectName = map.get("projectName").toString();
            String postJD = map.get("postJD").toString();
            String operator = map.get("operator").toString();
            int day = Integer.parseInt(map.get("days").toString());
            Date demandCreatedTime = (Date) map.get("demandCreatedTime");
            cal.setTime(demandCreatedTime);
            if (day != 0) {
                log.info(userMapper.getUserId(operator).getName() + "【提醒】您的【" + projectName + "】项目于" + cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月" + cal.get(Calendar.DAY_OF_MONTH) + "日" + "创建的【" + postJD + "】岗位的招聘需求距离结束日期还剩" + day + "天，请关注并处理！");
                fescoadQueryService.sendTextMessage(operator,"","【提醒】您的【"+projectName+"】项目于"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH) + 1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日"+"创建的【"+postJD+"】岗位的招聘需求距离结束日期还剩"+day+"天，请关注并处理！","招聘追踪");
            } else {
                log.info(userMapper.getUserId(operator).getName() + "【告警】您的【" + projectName + "】项目于" + cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH) + 1) + "月" + cal.get(Calendar.DAY_OF_MONTH) + "日" + "创建的【" + postJD + "】岗位的招聘需求将于今日到期，请项目经理在今天处理完成，过期将自动关闭需求！");
                fescoadQueryService.sendTextMessage(operator,"","【告警】您的【"+projectName+"】项目于"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH) + 1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日"+"创建的【"+postJD+"】岗位的招聘需求将于今日到期，请项目经理在今天处理完成，过期将自动关闭需求！","招聘追踪");
            }
        }
        log.info("招聘跟踪结束------------------------------------");
    }
}
